package edu.tamu.cse.lenss.sensor;

import java.util.List;

import android.content.Context;
import android.telephony.CellLocation;
import android.telephony.NeighboringCellInfo;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import edu.tamu.cse.lenss.utils.Logger;

public class CellTowerController {
	private static final String TAG = CellTowerController.class.getSimpleName();
	public enum CellTowerType{
		GSM, CDMA;
		@Override
		public String toString() {
			if(this.equals(GSM))
				return "GSM";
			else if(this.equals(CDMA))
				return "CDMA";
			else
				return null;
		}
	}
	
	private TelephonyManager tm;
	//private GsmCellLocation gsmLocation;
	//rivate CdmaCellLocation cdmaLocation;
	private CellTowerType type;
	private Context context;
	private CellTowerListener listener;
	
	public CellTowerController(Context context, CellTowerListener lis){
		this.context = context;
		this.tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		this.listener = lis;
		type = checkCellTowerType(context);
	}
	
	// Return if the device uses GSM or CDMA
	private static CellTowerType checkCellTowerType(Context cont){
		TelephonyManager tel = (TelephonyManager) cont.getSystemService(Context.TELEPHONY_SERVICE);
		String type = tel.getDeviceId();
		int len = type.trim().length();
		if(len == 15 || len == 17)
			return CellTowerType.GSM;
		else if(len == 11 || len == 18)
			return CellTowerType.CDMA;
		else{
			Logger.e(TAG, "Unknow Cell Network Type! " + type);
			return null;
		}
	}
	
	public void registerLocationUpdate(){
		if(type != null)
			tm.listen(stateListener, PhoneStateListener.LISTEN_CELL_LOCATION|PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
	}
	
	public void unregisterLocationUpdate(){
		tm.listen(stateListener, PhoneStateListener.LISTEN_NONE);
	}
	
	private SignalStrength strength;
	public PhoneStateListener stateListener = new PhoneStateListener(){
		private boolean firstUpdate = true;
		@Override
		public void onCellLocationChanged(CellLocation location) {
			if(type == CellTowerType.GSM){
				//Logger.v(TAG, "Current Cell Id: " + gsmLocation.getCid());
				listener.onGsmNewLocation((GsmCellLocation)location, tm.getNeighboringCellInfo(), strength);
				
			}
			else if(type == CellTowerType.CDMA){
				listener.onCdmaNewLocation((CdmaCellLocation)location, strength);
			}
		}

		@Override
		public void onSignalStrengthsChanged(SignalStrength signalStrength) {
			Logger.v(TAG, "Strength Change");
			strength = signalStrength;
		}
	};
	
	public static interface CellTowerListener{
		/**
		 * Strength may be null if it is not available
		 */
		public void onGsmNewLocation(GsmCellLocation location, List<NeighboringCellInfo> neighbor, SignalStrength strength);
		/**
		 * Strength may be null if it is not available
		 */
		public void onCdmaNewLocation(CdmaCellLocation location, SignalStrength strength);
	}
}
